import 'dart:async';

import 'package:flutter/material.dart';

/*class BlocDemoPage extends StatefulWidget {
  BlocDemoPage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _BlocDemoPageState createState() => _BlocDemoPageState();
}

class _BlocDemoPageState extends State<BlocDemoPage> {
  int _counter = 0;
  final StreamController<int> _streamController = StreamController<int>();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    _streamController.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print('_BlocDemoPageState.build');
    return Scaffold(
      appBar: AppBar(
        title: Text('Bloc 的基本使用'),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () {},
        ),
      ),
      body: Container(
        alignment: AlignmentDirectional.centerStart,
        color: Colors.white,
        margin: EdgeInsets.all(10),
        child: Column(
          children: <Widget>[
            RaisedButton(
              child: Text('点击'),
              onPressed: () {
                _streamController.sink.add(++_counter);
              },
            ),

            //这里只更新用 StreamBuilder包裹的组件，局部更新的一种实现方式
            StreamBuilder<int>(
              stream: _streamController.stream,
              initialData: _counter,
              builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
                return Text('12222=: ${snapshot.data} times');
              },
            ),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}*/

/*class BlocDemoPage extends StatefulWidget {
  BlocDemoPage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _BlocDemoPageState createState() => _BlocDemoPageState();
}

class _BlocDemoPageState extends State<BlocDemoPage> {
  // StreamController _controller = StreamController(); // 创建单订阅类型 `StreamController`

  //广播订阅 stream 与上面的区别就是：如果在 listen 监听之前，add数据，数据不会被监听到
  StreamController _controller = StreamController.broadcast();

  Sink _sink;
  StreamSubscription _subscription;

  @override
  void initState() {
    super.initState();

    _sink = _controller.sink;

    //只接受大于 10 的前 5 个数据，
    _subscription =
        _controller.stream.where((event) => event > 10).take(5).listen((event) {
      print('_BlocDemoPageState.initState event=${event}');
    });

    //_sink.add("A");
//    _subscription = _controller.stream.listen((event) {
//      print('_BlocDemoPageState.initState event=${event}');
//    });
    _sink.add(5);
    _sink.add(6);
    _sink.add(11);
    _sink.add(12);
    _sink.add(13);
    _sink.add(14);
    _sink.add(15);
    _sink.add(16);
    _sink.add(17);

//    _subscription.pause();
//    print('_BlocDemoPageState.initState11');
//    Future.delayed(Duration(seconds: 5), () {
//      print('_BlocDemoPageState.initState22');
//      _subscription.resume();
//    });
  }

  @override
  void dispose() {
    super.dispose();
    _sink.close();
    _controller.close();
    _subscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bloc 的基本使用'),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () {},
        ),
      ),
      body: Container(
        alignment: AlignmentDirectional.centerStart,
        color: Colors.white,
        margin: EdgeInsets.all(10),
        child: Column(
          children: <Widget>[
            Text("内容"),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}*/

class BlocDemoPage extends StatelessWidget {
  int _counter = 0;
  final StreamController<int> _streamController = StreamController<int>();

  @override
  Widget build(BuildContext context) {
    print('BlocDemoPage.buildddd');

    return Scaffold(
      appBar: AppBar(
        title: Text('BloC 的基本使用'),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () {
            _streamController.close();
            Navigator.of(context).pop();
          },
        ),
      ),
      body: Container(
        alignment: AlignmentDirectional.centerStart,
        color: Colors.white,
        margin: EdgeInsets.all(10),
        child: Column(
          children: <Widget>[
            RaisedButton(
              child: Text('点击'),
              onPressed: () {
                _streamController.sink.add(++_counter);
              },
            ),

            //这里只更新用 StreamBuilder包裹的组件，局部更新的一种实现方式
            StreamBuilder<int>(
              stream: _streamController.stream,
              initialData: _counter,
              builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
                return Text('12222=: ${snapshot.data} times');
              },
            ),
          ],
        ),
      ), // This
    );
  }
}
